<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class adv extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message = "";         
          $this->permit_lib->check_permit();
          $this->session->set_userdata(array('Url'=>uri_string()));
          $this->lang->load('adv');
          $this->load->model('adv_model','adv');
		  $this->load->helper('upload_file');
      }   
	  function index(){
		  $this->listadv();
	  }
      function listadv(){
         
        /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('adv_title'=>'Tiêu đề'),'adv/listadv/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
               
            }
            else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
            /*End  search*/
            
            $data['title'] = 'Danh sách quảng cáo';
            $data['add'] = 'adv/add';
            
            $config['base_url'] = base_url().'adv/listadv/';  
            $data['num'] = $this->adv->getNumAdv($arr_search);
            $config['total_rows']   =  $data['num'];
            $config['per_page']  =   '20';
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);   
            $data['pagination']    = $this->pagination->create_links();
            
            $data['list'] =   $this->adv->getAllAdv($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
            
            
           $this->_templates['page'] = 'adv/listadv';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                $this->site_lib->load($this->_templates['page'],$data); 
      }
      function add(){                         
            $data['title'] = 'Thêm quảng cáo';
            $this->form_validation->set_rules('adv_customer_id',lang('adv_customer'),'trim|required');
			$this->form_validation->set_rules('adv_page_id',lang('adv_page'),'trim|required');
            $this->form_validation->set_rules('adv_position_id',lang('adv_position'),'trim|required');
            
            $this->form_validation->set_rules('adv_title',lang('adv_name'),'trim|required');
            $this->form_validation->set_rules('adv_file_type',lang('adv_banner_file_type'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				$uploadDir = "../uploads/images/adv/vn/";
				$uploadDir_en = "../uploads/images/adv/en/";
                
                $adv_file_type =$this->input->post('adv_file_type');
                    
                if($adv_file_type=='FLA'){
                    $flag=4;
                }else{
                    $flag=1;
                }
				 // file vn
				 $files = $_FILES['adv_file'];
                 $img_name = settings_upload_avatar($files,$uploadDir,$flag);
				 // file en
				 $files_en = $_FILES['adv_file_en'];
                 $img_name_en = settings_upload_avatar($files_en,$uploadDir_en,$flag);
				 if($img_name!='-1' && $img_name_en!='-1'){
					 $arr_image = array('adv_file'=>$img_name,'adv_file_en'=>$img_name_en);
					 $data = $this->build_data($_POST,1);
					 $data  = array_merge($data,$arr_image);
					 if($this->adv->saveData(0,$data)){
						 $this->session->set_flashdata('message','Lưu thành công');
						 redirect('adv/listadv');
					 }else{
						 
						 $this->pre_message = 'Lưu không thành công';
					 }
				 }else{
					  $this->pre_message = 'File không đúng định dạng, vui lòng tải lại';
				 }
             }
          $data['message'] = $this->pre_message;
          $data['adv_customer']= $this->adv->getAllAdvCustomer();
          $data['list_page']=$this->adv->getAllAdvPage();
          $this->_templates['page'] = 'adv/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){                         
            $data['title'] = 'Sửa quảng cáo';
            $data['item'] = $this->adv->get_object($id);
           	$this->form_validation->set_rules('adv_customer_id',lang('adv_customer'),'trim|required');
			$this->form_validation->set_rules('adv_page_id',lang('adv_page'),'trim|required');
            $this->form_validation->set_rules('adv_position_id',lang('adv_position'),'trim|required');
            
            $this->form_validation->set_rules('adv_title',lang('adv_name'),'trim|required');
            $this->form_validation->set_rules('adv_file_type',lang('adv_banner_file_type'),'trim|required');
            
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				$uploadDir = "../uploads/images/adv/vn/";
				$uploadDir_en = "../uploads/images/adv/en/";
				$files = $_FILES['adv_file'];
				$files_en= $_FILES['adv_file_en'];
				
                $adv_file_type =$this->input->post('adv_file_type');
                    
                if($adv_file_type=='FLA'){
                    $flag=4;
                }else{
                    $flag=1;
                }
                
				if($files['name']!='')
					$img_name = settings_upload_avatar($files,$uploadDir,$flag);
				else
					$img_name =$data['item']->adv_file; 
				if($files_en['name']!='')
					$img_name_en = settings_upload_avatar($files_en,$uploadDir_en,$flag);
				else
					$img_name_en =$data['item']->adv_file_en; 
				if($img_name!='-1' && $img_name_en!='-1'){
					$arr_image = array('adv_file'=>$img_name,'adv_file_en'=>$img_name_en);
					$data = $this->build_data($_POST,1);
					$data  = array_merge($data,$arr_image);
					 if($this->adv->saveData($id,$data)){
						 $this->session->set_flashdata('message','Lưu thành công');
						 redirect('adv/listadv');
					 }else{
						 $this->pre_message = 'Lưu không thành công';
					 }
				}else{
					$this->pre_message = 'File không đúng định dạng, vui lòng tải lại';
				}
             }
          $data['message'] = $this->pre_message;
          $data['adv_customer']= $this->adv->getAllAdvCustomer();
          $data['list_page']=$this->adv->getAllAdvPage();
          $data['list_position']=$this->adv->getAdvPositionByPage($data['item']->adv_page_id);
          $this->_templates['page'] = 'adv/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->adv->deleted($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('adv/listadv/'.$page);
    }
    function dels(){
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->adv->deleted($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('adv/listadv/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
		
        $dataUpdate["adv_customer_id"]=trim($data["adv_customer_id"]);
        $dataUpdate["adv_page_id"]=trim($data["adv_page_id"]);
        $dataUpdate["adv_position_id"]=trim($data["adv_position_id"]);
		$dataUpdate["adv_title"]=trim($data["adv_title"]);
		$dataUpdate["adv_title_en"]=trim($data["adv_title_en"]);
		$dataUpdate["adv_url"]=trim($data["adv_url"]);
        $dataUpdate["adv_file_type"]=trim($data["adv_file_type"]);
		$dataUpdate["adv_link"]=trim($data["adv_link"]);
		$dataUpdate["adv_link_en"]=trim($data["adv_link_en"]);
        
        $dataUpdate["dt_start"]=format_date_sql($data["dt_start"]);
        $dataUpdate["dt_end"]=format_date_sql($data["dt_end"]);
        $dataUpdate["nb_order"]=trim($data["nb_order"]);
        
		if($flag==1)
           	$dataUpdate["dt_create"] = date('Y-m-d H:s:i');
        
        if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}
    function get_position(){
        $adv_page_id = $this->input->post('adv_page_id');
        $opt_begin='<select name="adv_position_id" class="w300"><option value="">Vị trí</option>';
        $opt = '';
        $opt_end='</select>';
        if($adv_page_id>0){
            $rs = $this->adv->getAdvPositionByPage($adv_page_id);
            foreach($rs as $item){
                $opt .='<option '.(($item->adv_page_id==$adv_page_id)?'selected="true"':'').' value="'.$item->adv_position_id.'">'.$item->adv_position_name.'</option>';
            }
        }
        echo $opt_begin.$opt.$opt_end;
    }

  }
?>
